package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjAdmClause;
import com.zb.entity.dto.ClauseDto;
import com.zb.mapper.ZjAdmClauseMapper;
import com.zb.service.ZjAdmClauseService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 后台条款表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-07-07
 */
@Service
public class ZjAdmClauseServiceImpl extends ServiceImpl<ZjAdmClauseMapper, ZjAdmClause> implements ZjAdmClauseService {

    // 新增或修改条款
    @Override
    public void save(ClauseDto clauseDto) {
        Assert.isTrue(Objects.equals(1, clauseDto.getType()) || Objects.equals(2, clauseDto.getType()) || Objects.equals(3, clauseDto.getType()),
                "不合法的条款类型", clauseDto.getType());

        if (null != clauseDto.getId()) {
            ZjAdmClause clause = new ZjAdmClause();
            clause.setId(clauseDto.getId());
            clause.setType(clauseDto.getType());
            clause.setContent(clauseDto.getContent());
            this.updateById(clause);
            return;
        }
        QueryWrapper wrapper = new QueryWrapper<ZjAdmClause>();
        wrapper.eq("type", clauseDto.getType());
        ZjAdmClause clause = this.getOne(wrapper);
        if (null != clause) {
            clause.setContent(clauseDto.getContent());
            clause.setGmtCreate(null);
            clause.setGmtUpdate(null);
            this.updateById(clause);
            return;
        }
        clause = new ZjAdmClause();
        clause.setId(IdWorker.getId());
        clause.setType(clauseDto.getType());
        clause.setContent(clauseDto.getContent());
        this.save(clause);
    }

    // 根据条款类型获取条款
    @Override
    public ClauseDto getClaus(Integer type) {
        Assert.isTrue(Objects.equals(1, type) || Objects.equals(2, type) || Objects.equals(3, type), "不合法的条款类型", type);

        QueryWrapper wrapper = new QueryWrapper<ZjAdmClause>();
        wrapper.eq("type", type);
        ZjAdmClause clause = this.getOne(wrapper);

        ClauseDto dto = new ClauseDto();
        if (null != clause) {
            dto.setId(clause.getId());
            dto.setType(type);
            dto.setContent(clause.getContent());
        }
        return dto;
    }
}
